
using Youz.Common.Helper;
using Youz.Common.DAL;
using Youz.Common.Entity;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;

namespace Youz.Common.BLL
{
    /// <summary>
    /// Right_RoleRight BLL Class
    /// </summary>
    public partial class Right_RoleRightBLL : BaseBLL
    {
        private const string CacheKey = "Right_RoleRight_MenuURLList";
        private Right_RoleRightDAL dal = new Right_RoleRightDAL();
        private static Right_RoleRightDAL dals = new Right_RoleRightDAL();

        public Right_RoleRightBLL()
        {
            base.MenuName = "安全管理";
            base.ModuleName = "許可權管理";
        }

        #region  成员方法
        public bool Add(string menuInfos, Guid role)
        {
            bool r = dal.Add(menuInfos, role);

            if (r)
            {
                CacheHelper.RemoveCache(CacheKey);
            }

            base.WriteOperationLog(OperationType.Update, r, string.Format("{0}, Menus={1}", role.ToString(), menuInfos));

            return r;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Right_RoleRight Get(Guid RRID)
        {
            return dal.Get(RRID);
        }

        /// <summary>
        /// 得到一个JSON对象
        /// </summary>
        public string GetJson(Guid RRID)
        {
            return Utility.GetJsonByObject(Get(RRID));
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet Gets(string sqlWhere)
        {
            return dal.Gets(sqlWhere);
        }

        public List<Right_RoleRight> Gets(Guid roleID)
        {
            return new EntityHelper<Right_RoleRight>().FillEntity(this.Gets(string.Format("RoleID='{0}'", roleID)));
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet Gets(int top, string sqlWhere, string filedOrder)
        {
            return dal.Gets(top, sqlWhere, filedOrder);
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string sqlWhere)
        {
            return dal.GetRecordCount(sqlWhere);
        }

        /// <summary>
        /// 获得数据列表分页
        /// </summary>
        public DataTable Gets(string sqlWhere, int pageindex, int pagesize, out int total, params SqlParameter[] cmdParms)
        {
            return dal.Gets(sqlWhere, pageindex, pagesize, out total, cmdParms);
        }


        /// <summary>
        /// 獲取指定角色的功能表
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        private static Hashtable GetsByMenuURL()
        {
            Hashtable ht = CacheHelper.GetCache(CacheKey) as Hashtable;

            if (ht == null)
            {
                DataTable dt = dals.GetsByMenuURL();

                if (dt != null && dt.Rows.Count > 0)
                {
                    ht = new Hashtable();
                    Dictionary<int, string> urlList = null;
                    Guid roleID;

                    foreach (DataRow row in dt.Rows)
                    {
                        roleID = TypeHelper.ConvertTo<Guid>(row["RoleID"]);
                        if (!ht.ContainsKey(roleID))
                        {
                            urlList = new Dictionary<int, string>();
                            ht.Add(roleID, urlList);

                            urlList.Add(9998, "");
                            urlList.Add(9999, "Dashboard.aspx");
                        }

                        urlList = ht[roleID] as Dictionary<int, string>;
                        urlList.Add((int)row["MenuID"], row["MenuURL"].ToString().Trim('/').ToLower());
                    }

                    CacheHelper.SetCache(CacheKey, ht);
                }
            }

            return ht;
        }

        /// <summary>
        /// 驗證登錄用戶是否有許可權訪問頁面
        /// </summary>
        /// <param name="roleID"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public static bool Auth(Guid roleID, string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                return false;
            }

            if (url == "/")
            {
                return true;
            }

            Hashtable ht = GetsByMenuURL();

            if (ht == null)
            {
                return true;
            }

            Dictionary<int, string> list = ht[roleID] as Dictionary<int, string>;

            if (list == null)
            {
                return false;
            }

            url = url.Trim('/').ToLower();

            var r = list.Any(m => !string.IsNullOrEmpty(m.Value) && url.Contains(m.Value.ToLower()));

            return r;
        }

        /// <summary>
        /// 驗證登錄用戶是否有許可權訪問頁面
        /// </summary>
        /// <param name="roleID"></param>
        /// <param name="menuID"></param>
        /// <returns></returns>
        public static bool Auth(Guid roleID, int menuID)
        {
            // return true;
            if (menuID == 0)
            {
                return false;
            }

            Hashtable ht = GetsByMenuURL();
            Dictionary<int, string> list = null;
            if (ht != null)
            {
                list = ht[roleID] as Dictionary<int, string>;
            }
            if (list == null)
            {
                return false;
            }

            return list.Any(m => m.Key == menuID);
        }

        /// <summary>
        /// 驗證登錄用戶是否有許可權訪問頁面
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public static bool Auth(Guid roleID)
        {
            string url = WebHelper.CurrentRawUrl;

            if (string.IsNullOrEmpty(url))
            {
                return false;
            }

            return Auth(roleID, url);
        }
        #endregion  成员方法
    }
}


  